package engine.display.Story.ActionItem 
{
	import engine.display.Story.ActionControl.TalkActionPlay;
	import engine.display.Story.StoryVo;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.geom.Point;

	/**
	 * ...移动类型动作
	 * @author WangHaiBo  www.woniu.com
	 */
	public class TalkMoveActionUI extends BaseAction 
	{
		private static var _self:TalkMoveActionUI;
		private var moveCount	:int 	=	0;
		private var moveEndCount:int	=	0;
		private var speed:Number			=	25;
		private var _actionMCArray:Array
		private var _actionPointArray:Array;
		private var speedNumArray:Array;
		public function TalkMoveActionUI() 
		{
			
		}
		public static function getInstance():TalkMoveActionUI
		{
			if (_self == null) 
			{
				_self	=	new TalkMoveActionUI();
			}
			return _self;
		}
		/**
		 * 显示信息
		 * @param	val
		 */
		override public function showInfo(val:StoryVo):void 
		{
			_actionMCArray		=	[];
			_actionPointArray	=	[];
				
			for(var j:int = 0; j < val.dataArray.length; j++)
			{
				var item:StoryVo	=	val.dataArray[j] as StoryVo;
				_actionMCArray.push(item.actionNPC);
				_actionPointArray.push(item.NpcEndPoint);
			}
			speedNumArray	=	[];
			if (_actionMCArray.length != 0) 
			{
				moveCount	=	_actionMCArray.length;
				for (var i:int = 0; i < moveCount; i++) 
				{
					var tempMC:TalkActionPlay	=	_actionMCArray[i] as TalkActionPlay;
					var endPoint:Point		=	_actionPointArray[i] as Point
					if (tempMC.x <= endPoint.x) 
					{
						tempMC.ImageAlign = 1;
						speedNumArray.push(speed);
					}
					else
					{
						tempMC.ImageAlign = 2;
						speedNumArray.push(-speed);
					}
				}
				this.addEventListener(Event.ENTER_FRAME, enterFrameHandle);
			}
		}
		/**
		 * 帧事件
		 * @param	e
		 */
		private function enterFrameHandle(e:Event):void
		{
			var tempMc:TalkActionPlay
			var endPoint:Point
			var tempSpeed:Number

			for (var i:int = 0; i < _actionMCArray.length; i++) 
			{
				tempMc		=	_actionMCArray[i]		as TalkActionPlay;
				endPoint	=	_actionPointArray[i]	as Point;
				tempSpeed	=	speedNumArray[i];
				if (tempMc.PlayStart != TalkActionPlay.RUN && tempMc.x != endPoint.x)  
				{
					tempMc.PlayStart = TalkActionPlay.RUN;
				}
				if (tempSpeed < 0) 
				{
					if (tempMc.x <= endPoint.x)
					{
						removeItem(i);
						break;
					}
				}
				else
				{
					if (tempMc.x >= endPoint.x) 
					{
						removeItem(i);
						break
					}
				}
				tempMc.x += tempSpeed
				tempMc.y	+= (Math.PI*Math.atan2(endPoint.y - tempMc.y,endPoint.x - tempMc.x)/180) * tempSpeed
			}
			if (_actionMCArray.length == 0 )
			{
				this.removeEventListener(Event.ENTER_FRAME, enterFrameHandle);
				actionOver();
			}
		}
		/**
		 * 删除索引资源
		 * @param	index
		 */
		private function removeItem(index:int):void
		{
			var tempMC:TalkActionPlay	=	_actionMCArray[index] as TalkActionPlay;	
			tempMC.PlayStart	=	TalkActionPlay.WAIT;
			_actionMCArray.splice(index,1);
			_actionPointArray.splice(index,1);
			speedNumArray.splice(index,1);
		}
		
	}

}